<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "qa".
 *
 * @property integer $id
 * @property string $uid
 * @property string $title
 * @property string $body
 * @property string $status
 * @property string $view
 * @property string $best_answer
 * @property string $created_at
 * @property string $updated_at
 */
class Qa extends \yii\db\ActiveRecord {

    public $tags;

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'qa';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['title', 'body'], 'required'],
            [['body', 'status'], 'string'],
            [['title', 'tags'], 'string', 'max' => 128]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'uid' => '用户ID',
            'title' => '标题',
            'body' => '内容',
            'status' => '状态',
            'view' => '浏览数',
            'best_answer' => '最佳答案',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'tags' => '标签',
        ];
    }

    /**
     * 获取关联标签ID
     * @return type
     */
    public function getQa_tag() {
        return $this->hasMany(Qa_tag::className(), ['id' => 'qid']);
    }

    /**
     * 获取标签列表
     * @param type $id
     * @return type
     */
    public function tags($id) {
        $new_tag_list = array();
        $qa_tag_list = Qa_tag::find()->where(['qid' => $id])->asArray()->all();
        foreach ($qa_tag_list as $row) {
            $tag_info = Tags::findOne(['id' => $row['tid']]);
            if ($tag_info) {
                $new_tag_list[] = $tag_info;
            }
        }
        return $new_tag_list;
    }

    /**
     * 获取评论总数
     * @return type
     */
    public function getComments() {
        return $this->hasMany(Comments::className(), ['rid' => 'id'])->onCondition(['type' => 'qa', 'status' => '1'])->count();
    }

    public function getAuthor() {
        return $this->hasOne(Users::className(), ['id' => 'uid']);
    }

    /**
     * 在插入之前操作
     * 记录用户ID、创建、更新时间
     * @param type $insert
     * @return type
     */
    public function beforeSave($insert) {
        if ($this->isNewRecord) {
            $this->uid = Yii::$app->user->identity->id;
            $this->status = '1';
            $this->created_at = time();
        } else {
            $this->updated_at = time();
        }
        return parent::beforeSave($insert);
    }

    /**
     * 在写入数据之后进行操作
     * 增加声望
     * @param type $insert
     */
    public function afterSave($insert, $changedAttributes) {
        if ($this->isNewRecord) {
            Users::add_prestige(2, Yii::$app->user->identity->id); //提问增加2点声望
        }
        parent::afterSave($insert, $changedAttributes);
    }

    /**
     * 检查用户是否具有该问答的操作权限
     * @param int $id
     * @param int $uid
     * @throws \yii\web\ForbiddenHttpException
     */
    public function check_access($id, $uid) {
        $article = Qa::findOne($id);
        if ($article->uid != $uid) {
            throw new \yii\web\ForbiddenHttpException('你没有权限进行操作！');
        }
    }

    /**
     * 浏览数增加
     * @param int $id
     */
    public function view_add($id) {
        $qa = Qa::findOne($id);
        $view = $qa->view + 1;
        $qa->view = $view;
        $qa->save();
    }

}
